/*
  学习目标：React中的ref获取组件实例对象
  
*/

import React, { Component } from 'react';

export default class App extends Component {
  // 1. React.createRef() 创建ref对象
  childRef = React.createRef();

  handleClick = () => {
    // 3. 通过this.childRef.current拿到组件实例对象
    this.childRef.current.sayHello();
  };

  render() {
    return (
      <div>
        <button onClick={this.handleClick}>点击说hello</button>
        {/* 2. 给子组件设置ref属性，绑定ref对象 */}
        <Child ref={this.childRef}></Child>
      </div>
    );
  }
}

class Child extends Component {
  sayHello = () => {
    alert('hello');
  };

  render() {
    return (
      <div>
        <h2>我是Child组件</h2>
      </div>
    );
  }
}
